package cn.power.win.win_power.utils;


import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.os.Looper;
import android.util.Log;


import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.power.win.win_power.MyApp;

/*
* 打印crash的log
* */

public class MyCrashHandler implements Thread.UncaughtExceptionHandler {
    private static MyCrashHandler sMyCrashHandler = new MyCrashHandler();

    private Context mContext;

    public static MyCrashHandler getInstance() {
        return sMyCrashHandler;
    }

    public void init(Context context) {
        Thread.setDefaultUncaughtExceptionHandler(this);
        mContext = context;

    }

    /**
     * thread为未捕获异常的线程， e为未捕获的异常
     */
    @Override
    public void uncaughtException(Thread t, Throwable e) {

        StringBuffer sb = new StringBuffer();
        String time = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒").format(new Date(System.currentTimeMillis()));

        Writer writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);

        //收集当前的手机信息
        PackageManager pm = mContext.getPackageManager();
        try {
            PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(),PackageManager.GET_ACTIVITIES);
            printWriter.print("app version: ");
            printWriter.print(pi.versionName);
            printWriter.print("-");
            printWriter.println(pi.versionCode);

            printWriter.print("OS VERSION： ");
            printWriter.print(Build.VERSION.RELEASE);
            printWriter.print("-");
            printWriter.println(Build.VERSION.SDK_INT);

            printWriter.print("制造商： ");
            printWriter.println(Build.MANUFACTURER);

        } catch (PackageManager.NameNotFoundException e1) {
            e1.printStackTrace();
        }

        // 手机当前的异常信息
        e.printStackTrace(printWriter);
        printWriter.close();
        String result = writer.toString();
        sb.append(result);
        // 写入异常信息到文件中
        try {
            long timestamp = System.currentTimeMillis();
            String fileName = "crash_" + time + ".log";
            File file = new File(Environment.getExternalStorageDirectory(),
                    fileName);
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(sb.toString().getBytes());
            Log.e("Exceptions",sb.toString());
            fos.close();

        } catch (Exception ex) {
            Log.e("error","Error :"+ex.toString());
        }

        // 使用 Toast 来显示异常信息
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
         //       Toast.makeText(MyApp.context, "Sorry app happened error", Toast.LENGTH_LONG).show();
                Looper.loop();
            }
        }.start();
    }

}
